<template>

<el-card class="box-card">
  
    <el-form ref="form"
             :model="form"
             inline>
      <el-form-item prop="search">
        <el-input v-model="form.search"
                  clearable
                  @clear="search"
                  style="width: 200px"
                  prefix-icon="search"
                  placeholder="输入名称" />
      </el-form-item>

      <el-form-item>
        <el-button type="success"
                   icon="search"
                   size="small"
                   @click="search(form)">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-button 
               type="primary"
               style="margin-bottom: 20px"
               icon="plus"
               size="small"
               @click="createPlatformAccount()">新增</el-button>

    <!--表单组件-->
    <el-table ref="table"
              :data="tableData"
              style="width: 100%"
              row-key="id"
              highlight-current-row>
      <el-table-column prop="name"
                       label="名称"
                       min-width="100"
                       show-overflow-tooltip />
      <el-table-column prop="accesskey"
                       label="accesskey"
                       min-width="100"
                       show-overflow-tooltip />
      <!-- <el-table-column prop="secret"
                       label="secret"
                       min-width="100"
                       show-overflow-tooltip
                        /> -->
      <el-table-column prop="platform_id.name"
                       label="平台"
                       min-width="100"
                       show-overflow-tooltip />

      <el-table-column prop="desc"
                       label="备注"
                       min-width="100"
                       show-overflow-tooltip />

      <el-table-column prop="gmt_create"
                       label="创建时间" />
      <el-table-column fixed="right"
                       align="center"
                       label="操作"
                       width="220">
        <template #default="scope">
          <el-button 
                     type="primary"
                     icon="edit"
                     size="small"
                     @click="updatePlatformAccount(scope.row)">编辑</el-button>
          <el-button 
                     type="danger"
                     icon="delete"
                     size="small"
                     @click="deletePlatformAccount(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <el-pagination :current-page="1"
                   :page-sizes="[10, 20, 50, 100]"
                   :page-size="10"
                   :total="total"
                   layout="total, sizes, prev, pager, next, jumper"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange" />

    <cuForm :dialog-visible="cuDialogVisible"
            :cur-id="curId"
            @close="close"
            @search="search" />
  
</el-card>
</template>
<script>
import cuForm from "./components/cuForm";
import {
  deletePlatformAccount,
  getPlatformAccounts,

} from "@/api/cloudPlatform/platform";

export default {
  name: "platforms-account",
  components: { cuForm },
  data () {
    return {
      form: {
        page: 1,
        size: 10,
        search: "",
        ordering: "",
      },
      tableData: [],
      total: 0,
      isAllSelect: false,
      multipleSelection: [],
      // cuForm数据
      cuDialogVisible: false,
      curId: null,
    };
  },
  created () {
    this.search();
  },
  methods: {
    // 获取列表/搜索功能
    search () {
      getPlatformAccounts(this.form).then((res) => {
        this.tableData = res.data.results;
        this.total = res.data.count;
      });
    },
    // 重置
    resetForm () {
      this.$refs.form.resetFields();
      this.search();
    },
    // 删除
    deletePlatformAccount (row) {
      this.$confirm(
        '此操作将删除 "' + row.name + '" ' + " , 是否继续？",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        deletePlatformAccount(row.id).then((res) => {
            res.data
          this.$message({
            message: "删除" + row.name + "成功",
            type: "success",
          });
          // 刷新table
          this.search();
        });
      });
    },

    // cuForm子组件
    createPlatformAccount () {
      this.cuDialogVisible = true;
    },
    updatePlatformAccount (row) {
      this.curId = row.id;
      this.cuDialogVisible = true;
    },
    close () {
      this.cuDialogVisible = false;
      this.curId = null;
    },
    // 分页
    handleSizeChange (val) {
      this.form.size = val;
      this.search();
    },
    handleCurrentChange (val) {
      this.form.page = val;
      this.search();
    },
  },
};
</script>
